var currDT;
var aryDay = new Array("日","一","二","三","四","五","六");//显示星期
var wcolor=new Array("#cef3db","#ffe2d8","#e2ccf1","#fcd6de","#d0eff6","#f4cff0","#bfe7fd");
var lastDay;//页面显示的最后一天
var firstDay;//页面显示的第一天
var nmonth;

//初始化日期加载
function initDate(t) {
	var prev="initDate('"+getlastmonth(t)+"')";
	var next="initDate('"+getnextmonth(t)+"')";
	var u=t.replace(/-/g,"/");
	var s=$("#ndate").val();
	s=s.replace(/-/g,"/");
	currDT = new Date(u);
	var cdt=new Date(u);
	var cot=new Date(s);
	var a=currDT.getMonth();
	if(parseInt(a)<9){
		nmonth=currDT.getFullYear()+'年0'+(parseInt(a)+1)+'月';
	}else{
		nmonth=currDT.getFullYear()+'年'+(parseInt(a)+1)+'月';
	}
	$(".rlshow").html(nmonth);
	$(".rlprev").attr("onclick",prev);
	$(".rlnext").attr("onclick",next);
	var dw = currDT.getDay();//从Date对象返回一周中的某一天(0~6)
	var tdDT;//日期
	var crtDT=currDT.getDate();
	var b;
	//处理JSON数组{"id":0,"date":"2014-4-29","wid":0,"tid":7}
	//本函数废弃id
//	if (lplan!=''){
//		lplan=eval('('+lplan+')');
//		var n=lplan.split('|');
//	}

	//在表格中显示一月的日期
	var objTB = document.getElementById("mytable");//取得表格对象
	cdt.setDate(1);
	var mst=cdt.getDay();
	if (mst>4){
		var zrow=6;
		$("#mt_yc").show();
	}else{
		var zrow=5;
		$("#mt_yc").hide();
	}
	cdt.setDate(0-mst);
	var sd=cdt.getFullYear()+'-'+(cdt.getMonth()+1)+'-'+cdt.getDate();
	for(var i=1;i<=zrow;i++){
		for(var j=0;j<7;j++){
			cdt.setDate(cdt.getDate()+1);

			if (cdt.getMonth()!=currDT.getMonth()){
				if(cdt.getMonth()==cot.getMonth() && cdt.getDate()==cot.getDate() && cdt.getFullYear()==cot.getFullYear()){
					objTB.rows[i].cells[j].style.background='#fef0d7';
				}else{
					objTB.rows[i].cells[j].style.background='#eee';
				}
			}else{
				if(cdt.getMonth()==cot.getMonth() && cdt.getDate()==cot.getDate() && cdt.getFullYear()==cot.getFullYear()){
					objTB.rows[i].cells[j].style.background='#fef0d7';
				}else{
					objTB.rows[i].cells[j].style.background='#fff';
				}
			}
			objTB.rows[i].cells[j].innerHTML='<div class="kdiv"><div class="kcdata" id="'+cdt.getFullYear()+'-'+cdt.getMonth()+'-'+cdt.getDate()+'"></div><div class="kcdate">'+cdt.getDate()+'</div></div>';
		}
	}
	var ed=cdt.getFullYear()+'-'+(cdt.getMonth()+1)+'-'+cdt.getDate();
	initkcdata(t,sd,ed);
}

function initkcdata(t,sd,ed){
	var color=new Array("#56bfb5","#357b7a","#fbb336","#eb6100")
	var rl,w,h;
	var cid=$("#cid").val();
	if(cid==0){
		return;
	}
	var rid=$("#rid").val();
	var uid=$("#uid").val();
	$.post("index.php?ac=rclass&ts=kcdata",
			{
				date:t,
				cid:cid,
				rid:rid,
				uid:uid,
				sd:sd,
				ed:ed
			},
			function(msg){
//				alert(msg);
//				return;
				if(msg!=''){
					var arr=eval('('+msg+')');
					for(var k in arr){
						h='';
						for(var i=0;i<arr[k].length;i++){
							h+='<div class="kimg"';
							if(arr[k][i]['level']>4 && arr[k][i]['level']<30){
								h+=' style="background:'+color[2]+';"';
							}else if(arr[k][i]['level']>30 && arr[k][i]['level']<50){
								h+=' style="background:'+color[3]+';"';
							}else if(arr[k][i]['level']>50){
								h+=' style="background:'+color[4]+';"';
							}
							h+='></div><div class="ktt" onclick=location.href="index.php?ac=rclass&ts=view&id='+arr[k][i]['id']+'&tm='+arr[k][i]['dtime']+'">'+arr[k][i]['kcname']+'</div>';
							h+='<div class="kbody">'+arr[k][i]['stime']+'-'+arr[k][i]['etime']+'&nbsp;&nbsp;'+arr[k][i]['kclevel']+'</div>';
						}
						$("#"+k).html(h);
					}
				}
			});
}

function initDatewk(t){
	var prev="initDatewk('"+getlastweek(t)+"')";
	var next="initDatewk('"+getnextweek(t)+"')";
	var u=t.replace(/-/g,"/");
	var s=$("#ndate").val();
	s=s.replace(/-/g,"/");
	currDT = new Date(u);
	var cdt=new Date(u);
	var cot=new Date(s);
	var a=currDT.getMonth();
	if(parseInt(a)<9){
		nmonth=currDT.getFullYear()+'年0'+(parseInt(a)+1)+'月';
	}else{
		nmonth=currDT.getFullYear()+'年'+(parseInt(a)+1)+'月';
	}
	$(".rlshow").html(nmonth);
	$(".rlprev").attr("onclick",prev);
	$(".rlnext").attr("onclick",next);
	var dw = currDT.getDay();//从Date对象返回一周中的某一天(0~6)
	var tdDT;//日期
	var crtDT=currDT.getDate();
	var b;
	if(dw==0){
		var si=-7;
	}else{
		var si=0-dw;
	}


	//在表格中显示一月的日期
	var objTB = document.getElementById("mytable");//取得表格对象
	cdt.setDate(cdt.getDate()+si);
	var mn,dy;
	for(var i=0;i<=1;i++){
		cdt=new Date(u);
		cdt.setDate(cdt.getDate()+si);
		for(var j=0;j<7;j++){
			cdt.setDate(cdt.getDate()+1);

			b=j+1;
			if(b>=7){
				b=0;
			}
			if(i==1){
				if (cdt<cot){
					objTB.rows[i].cells[j].style.background='#eee';
				}else{
					if(cdt.getMonth()==cot.getMonth() && cdt.getDate()==cot.getDate() && cdt.getFullYear()==cot.getFullYear()){
						objTB.rows[i].cells[j].style.background='#fef0d7';
					}else{
						objTB.rows[i].cells[j].style.background='#fff';
					}
				}
				mn=cdt.getMonth()+1;
				objTB.rows[i].cells[j].id=cdt.getFullYear()+'-'+mn+'-'+cdt.getDate();
				objTB.rows[i].cells[j].innerHTML='&nbsp';
			}else{
				mn=cdt.getMonth()+1;
				if(mn<10){
					mn='0'+mn;
				}
				dy=cdt.getDate();
				if(dy<10){
					dy='0'+dy;
				}
				objTB.rows[i].cells[j].innerHTML=cdt.getFullYear()+'-'+mn+'-'+dy+'(周'+aryDay[b]+')';
			}
		}
	}
	var ed=cdt.getFullYear()+'-'+(cdt.getMonth()+1)+'-'+cdt.getDate();
	initkcdatawk(t,ed);
}
function initkcdatawk(t,ed){
	var color=new Array("#56bfb5","#357b7a","#fbb336","#eb6100","#ff00ff")
	var rl,w,h;
	var uid=$("#uid").val();
	$.post("index.php?ac=wclass&ts=kcdata",
			{
				date:t,
				uid:uid,
				ed:ed
			},
			function(msg){
				if(msg!=''){
					var arr=eval('('+msg+')');
					for(var k in arr){
						h='';
						for(var i=0;i<arr[k].length;i++){
							h+='<div class="kcb"><div class="wlkimg"';
							if(arr[k][i]['lvid']==1){
								h+=' style="background:'+color[0]+';"';
							}else if(arr[k][i]['lvid']==2){
								h+=' style="background:'+color[1]+';"';
							}else if(arr[k][i]['lvid']==3){
								h+=' style="background:'+color[2]+';"';
							}
							if(arr[k][i]['isxs']==1){
								h+='></div><div class="wlktt" onclick=location.href="index.php?ac=wclass&ts=view&id='+arr[k][i]['id']+'">L'+arr[k][i]['lsid']+'&nbsp;'+arr[k][i]['kcname']+'</div>';
							}else{
								h+='></div><div class="wlktt">试听课</div>';
							}
//							h+='<div class="wlkbody1">'+arr[k][i]['rname']+'</div></div><div class="con"><div class="wlkbody1">主教：'+arr[k][i]['fuser']+'</div>';
							h+='</div><div class="kcb"><div class="wlkbody1">宝宝：'+arr[k][i]['bname']+'</div><div class="wlkbody1">老师：'+arr[k][i]['fuser']+'</div>';
							h+='</div><div class="kcb"><div class="wlkbody">'+arr[k][i]['stime']+'-'+arr[k][i]['etime']+'</div><div class="wlkbody">Step '+arr[k][i]['lvid']+'</div></div>';
							h+='</div><div class="kcb"><div class="wlkbody">'+arr[k][i]['addr']+'</div></div>';
							h+='</div><div class="kcb" style="margin-bottom:10px;"><div class="wlkbody1" style="color:'+color[arr[k][i]['iskq']]+'">'+arr[k][i]['kq']+'</div>';
							if(arr[k][i]['iskq']==0 && arr[k][i]['isq']>0){
								h+='<div class="wlkbody2" onclick="kcbop(this,'+arr[k][i]['id']+','+arr[k][i]['dtime']+')">操作</div>';
							}
							h+='</div>';
						}
						$("#"+k).html(h);
					}
				}
			});
}

function xskcb(bid,blid,dt){
	$.post("index.php?ac=addajax&ts=xskcbhtm",
			{
				bid:bid,
				blid:blid
			},
			function(msg){
				var h=msg.split('|||');
				$(".ctop font").text(h[0]);
				$(".cw").html(h[1]);
				initDatexs(dt,bid,blid);
			});
	$("#cwin .cmain").css({"left":"15%","width":"70%","top":"10%","height":"80%"});
	var h=parseInt($(document).height()*0.8);
	$(".cw").css("height",h-48);
	$(".xsrl").find("td").css("height","90px");
	$("#cwin").show();
}
function initDatexs(t,bid,blid) {
	var prev="initDatexs('"+getlastmonth(t)+"',"+bid+","+blid+")";
	var next="initDatexs('"+getnextmonth(t)+"',"+bid+","+blid+")";
	var u=t.replace(/-/g,"/");
	var s=$("#ndate").val();
	s=s.replace(/-/g,"/");
	currDT = new Date(u);
	var cdt=new Date(u);
	var cot=new Date(s);
	var a=currDT.getMonth();
	if(parseInt(a)<9){
		nmonth=currDT.getFullYear()+'年0'+(parseInt(a)+1)+'月';
	}else{
		nmonth=currDT.getFullYear()+'年'+(parseInt(a)+1)+'月';
	}
	$(".rlshow").html(nmonth);
	$(".rlprev").attr("onclick",prev);
	$(".rlnext").attr("onclick",next);
	var dw = currDT.getDay();//从Date对象返回一周中的某一天(0~6)
	var tdDT;//日期
	var crtDT=currDT.getDate();
	var b;
	//处理JSON数组{"id":0,"date":"2014-4-29","wid":0,"tid":7}
	//本函数废弃id
//	if (lplan!=''){
//		lplan=eval('('+lplan+')');
//		var n=lplan.split('|');
//	}

	//在表格中显示一月的日期
	var objTB = document.getElementById("mytable");//取得表格对象
	cdt.setDate(1);
	var mst=cdt.getDay();
	if (mst>4){
		var zrow=6;
		$("#mt_yc").show();
	}else{
		var zrow=5;
		$("#mt_yc").hide();
	}
	cdt.setDate(0-mst);
	var sd=cdt.getFullYear()+'-'+(cdt.getMonth()+1)+'-'+cdt.getDate();
	for(var i=1;i<=zrow;i++){
		for(var j=0;j<7;j++){
			cdt.setDate(cdt.getDate()+1);

			if (cdt.getMonth()!=currDT.getMonth()){
				if(cdt.getMonth()==cot.getMonth() && cdt.getDate()==cot.getDate() && cdt.getFullYear()==cot.getFullYear()){
					objTB.rows[i].cells[j].style.background='#fef0d7';
				}else{
					objTB.rows[i].cells[j].style.background='#eee';
				}
			}else{
				if(cdt.getMonth()==cot.getMonth() && cdt.getDate()==cot.getDate() && cdt.getFullYear()==cot.getFullYear()){
					objTB.rows[i].cells[j].style.background='#fef0d7';
				}else{
					objTB.rows[i].cells[j].style.background='#fff';
				}
			}
			objTB.rows[i].cells[j].innerHTML='<div class="kdiv"><div class="kcdata" id="'+cdt.getFullYear()+'-'+cdt.getMonth()+'-'+cdt.getDate()+'"></div><div class="kcdate">'+cdt.getDate()+'</div></div>';
		}
	}
	var ed=cdt.getFullYear()+'-'+(cdt.getMonth()+1)+'-'+cdt.getDate();
	initkcdataxs(t,sd,ed,bid,blid);
}
function initkcdataxs(t,sd,ed,bid,blid){
	var color=new Array("#56bfb5","#357b7a","#fbb336","#eb6100")
	var rl,w,h;
	$.post("index.php?ac=contract&ts=kcdata",
			{
				date:t,
				sd:sd,
				ed:ed,
				bid:bid,
				blid:blid
			},
			function(msg){
				if(msg!=''){
					var arr=eval('('+msg+')');
					for(var k in arr){
						h='';
						for(var i=0;i<arr[k].length;i++){
							h+='<div class="kimg"';
							if(arr[k][i]['level']>4 && arr[k][i]['level']<30){
								h+=' style="background:'+color[2]+';"';
							}else if(arr[k][i]['level']>30 && arr[k][i]['level']<50){
								h+=' style="background:'+color[3]+';"';
							}else if(arr[k][i]['level']>50){
								h+=' style="background:'+color[4]+';"';
							}
							h+='></div><div class="ktt">'+arr[k][i]['kcname']+'</div>';
							h+='<div class="kbody flft">'+arr[k][i]['lsnm']+'</div>';
							h+='<div class="kbody flft">'+arr[k][i]['stime']+'-'+arr[k][i]['etime']+'&nbsp;&nbsp;'+arr[k][i]['fuser']+'</div>';
						}
						$("#"+k).html(h);
					}
				}
			});
}

function getlastmonth(t){
	t=t.replace(/-/g,"/");
	var cdt=new Date(t);
	var m=cdt.getMonth();
	var n,y;
	if(m==0){
		n=12;
		y=cdt.getFullYear()-1;
	}else{
		n=m;
		y=cdt.getFullYear();
	}
	return y+'-'+n+'-1';
}
function getnextmonth(t){
	t=t.replace(/-/g,"/");
	var cdt=new Date(t);
	var m=cdt.getMonth();
	var n,y;
	if(m==11){
		n=1;
		y=cdt.getFullYear()+1;
	}else{
		n=m+2;
		y=cdt.getFullYear();
	}
	return y+'-'+n+'-1';
}
function getlastweek(t){
	t=t.replace(/-/g,"/");
	var cdt=new Date(t);
	cdt.setDate(cdt.getDate()-7);
	var month=cdt.getMonth()+1;
	return cdt.getFullYear()+'-'+month+'-'+cdt.getDate();
}
function getnextweek(t){
	t=t.replace(/-/g,"/");
	var cdt=new Date(t);
	cdt.setDate(cdt.getDate()+7);
	var month=cdt.getMonth()+1;
	return cdt.getFullYear()+'-'+month+'-'+cdt.getDate();
}

$(document).ready(function(){
	$(".kcdatashow").hover(
		function(){
			$(this).css("height","auto");
			$(this).css("background","#eee");
			$(this).css("z-index","11");
	},
		function(){
			$(this).css("height","100%");
			$(this).css("background","");
			$(this).css("z-index","10");
	})
	//每日签到表
	$(".djkcbm").toggle(
		function(){
			$(this).parent().next(".djqdb").show();
			$(this).css("background","#ccc url(/app/home/html/images/up1.png) 95% 9px no-repeat");
		},
		function(){
			$(this).parent().next(".djqdb").hide();
			$(this).css("background","#ccc url(/app/home/html/images/down1.png) 95% 9px no-repeat");
		}
	)
})

function editrlkc(id,tm){
	$(".editrlkc").children("font").hide();
	$(".editrlkc").children(".dishidden").show();
	$(".kcedit").attr("onclick","saverlkc("+id+","+tm+")").text("保存");
}
function ckrkcroom(cid){
	var rid=$("[name='roomid']").val();
	var wid=$("#isw").val();
	$.post("index.php?ac=ajax&ts=ckrkcroom",
			{
				rid:rid,
				wid:wid,
				cid:cid
			},
			function(msg){
				if(msg!=''){
					var s=msg.split('|');
					$("#stime").html(s[0]);
					$("#etime").html(s[1]);
				}
			});
}
function ckrkctm(cid){
	var s=$("#ddate").val();
	var t=s.replace(/-/g,"/");
	var t=new Date(t);
	var isw=t.getDay();
	$("#isw").val(isw);
	ckrkcroom(cid);
}
function saverlkc(id,tm){
	$.post("index.php?ac=wclass&ts=saverlkc",
			{
				id:id,
				tm:tm,
				classname:$("#classname").val(),
				rid:$("#roomid").val(),
				fuid:$("[name='fuid']").val(),
				fuser:$("[name='fuser']").val(),
				zuser:$("[name='zuser']").val(),
				zuid:$("[name='zuid']").val(),
				ddate:$("#ddate").val(),
				stid:$("[name='stid']").val(),
				etid:$("[name='etid']").val(),
				kclevel:$("#kclevel").val()

			},
			function(msg){
				if(isNaN(msg)){
					alert(msg)
				}else{
					alert('编辑成功！');
					location.href='index.php?ac=wclass&ts=view&id='+id+'&tm='+msg;
				}
			});
}
function delrkc(id,tm){
	$.post("index.php?ac=wclass&ts=delrkc",
			{
				id:id,
				tm:tm

			},
			function(msg){
				location.href='index.php?ac=wclass';
			});
}

function editkcap(id,tm){
	$(".kcap").find("font").hide();
	$(".kcap").find(".dishidden").show();
	$(".kcapbut").attr("onclick","savekcap("+id+","+tm+")").text('保存');
}
function addkcap(u){
	var b=$(u).parent("div").attr("tid");
	var t=parseInt(b)+1;
	var h='<div class="con market'+t+'" tid="'+t+'">';
	h+='<div class="con3 adddel'+t+'"><input class="inputkw gymsub" type="text" style="width:500px;" name="content[]" placeholder="输入内容"></div>';
	h+='<div class="con3 hd" onclick="delkcap(this)">删除</div>';
	h+='<div class="con3 hd addmk" onclick="addkcap(this)">添加安排</div></div>';
	$(".addmk").remove();
	$(".market"+b).after(h);
}
function delkcap(u){
	var b=$(u).parent("div").attr("tid");
	if($(u).parent("div").children().is(".addmk")==true){
		var t=parseInt(b)-1;
		while($(".market"+t).length==0){
			t--;
		}
		var h=$(".market"+t).html()+'<div class="con3 hd addmk" onclick="addkcap(this)">添加安排</div>';
		$(".market"+t).html(h)
	}
	$(u).parent("div").remove();
}
function savekcap(id,tm){
	if(!checksubmit()){
		return;
	}
	var arr=new Array();
	$("[name='content[]']").each(function(){
		arr.push($(this).val());
	})
	var s=arr.join('|||');
	var kcl=$("#kclevelap").val();
	$.post("index.php?ac=wclass&ts=savekcap",
			{
				id:id,
				tm:tm,
				content:s,
				kclevel:kcl,
				unitid:$("[name='unitid']").val(),
				lsid:$("[name='lsid']").val()
			},
			function(msg){
				location.reload();
			});
}
function delkcapall(id,tm){
	$.post("index.php?ac=wclass&ts=delkcapall",
			{
				id:id,
				tm:tm

			},
			function(msg){
				location.reload();
			});
}

//每日上课登记
function showdj(u){
	$(".djdata").show();
	$(u).attr("onclick","hidedj(this)").text("收起");
}
function hidedj(u){
	$(".djdata").hide();
	$(u).attr("onclick","showdj(this)").text("展开");
}
function kcqd(u,id,tm,uid,ty){
	var x=$(u).offset().left;
	var y=$(u).offset().top;
	$.post("index.php?ac=ajax&ts=kcqd",
			{
				id:id,
				tm:tm,
				uid:uid,
				ty:ty
			},
			function(msg){
				if(msg!=''){
					$("#mt_ad").show().html(msg);
					$("#mt_ad").css("left",x+30);
					$("#mt_ad").css("top",y-50);
					$("#mt_ad").css("width","40px");
					$("#mt_ad").css("height","70px");
				}
			});
}
function selkcqd(u,id,tm,uid,ty){
	var vu=$(u).attr("vid");
	var idstr=id+'-'+tm+'-'+uid+'-'+ty;
	if(vu==2){
		$("#"+idstr).css("color","#0000ff");
	}else if(vu==3){
		$("#"+idstr).css("color","#ff0000");
	}else{
		$("#"+idstr).css("color","#646464");
	}
	$.post("index.php?ac=ajax&ts=selkcqd",
			{
				id:id,
				tm:tm,
				uid:uid,
				ty:ty,
				vu:vu
			},
			function(msg){
				$("#"+idstr).text($(u).text());
				$("#mt_ad").hide();
			});
}
function djprev(s,t){
	if(s<0){
		return;
	}
	var n=$("#roomnum").val();
	$(".djroom"+s).show();
	$(".djroom"+t).hide();
	var h;
	if(s==0){
		h='';
	}else{
		s--;
		t--;
		h="djprev("+s+","+t+")";
	}
	$(".djttabsl").attr("onclick",h);
	$(".djttabsr").attr("onclick","djnext("+s+","+t+")");
}
function djnext(s,t){
	var n=$("#roomnum").val();
	if(t>=n){
		return;
	}
	$(".djroom"+s).hide();
	$(".djroom"+t).show();
	var h;
	if(t==n-1){
		h='';
	}else{
		s++;
		t++;
		h="djnext("+s+","+t+")";
	}
	$(".djttabsr").attr("onclick",h);
	$(".djttabsl").attr("onclick","djprev("+s+","+t+")");
}

